package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.PosterService;
import cn.piaoyi.service.UserInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.interceptor.RemoteAddrAware;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.servlet.UserSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class Login extends BaseAction implements SessionAware, RemoteAddrAware{
	private static final Log logger = LogFactory.getLog(Login.class);
	
	private UserInfoService userInfoService;
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	private String remoteAddr;
	private Map session;
	
	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	
	private String account;
	private String password;
	private String tourl;

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public String getTourl() {
		return tourl;
	}

	public void setTourl(String tourl) {
		this.tourl = tourl;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		this.setAction("login");
		return SUCCESS;
	}
	
	public String pop(){
		this.setAction("poplogin");
		return "pop";
	}
	
	/**
	 * 登陆
	 */
	public String login(){
		UserInfo ui = this.getUserInfoService().findUserInfoByAccount(this.account);
		if(ui == null){
			this.addFieldError("error", this.getText("error.login.user"));
	        return SUCCESS;
		}
		//判断
		if(ui.getValidated()!=1){
			this.addFieldError("error", this.getText("error.login.validated"));
	        return SUCCESS;
		}
		//判断密码
		if(!Util.hash(this.password).equalsIgnoreCase(ui.getPassword())){
			this.addFieldError("error", this.getText("error.login.user"));
			return SUCCESS;
		}
		ui.setLastLoginTime(new Date());
		ui.setLastLoginIP(this.getRemoteAddr());
		try {
			this.getUserInfoService().updateUserInfo(ui);
		} catch (PiaoyiException e) {
            this.addActionError(this.getText("error.login.updaterror"));
            return ERROR;
        }
		UserSession us = new UserSession();
		us.setUid(ui.getId());
		us.setAccount(this.account);
		this.getSession().put(Constant.USER_SESSION_KEY, us);
		if(StringUtils.isNotBlank(this.tourl)){
			return "gourl";
		}
		return "index";
	}

	/**
	 * 登陆
	 */
	public String poplogin(){
		UserInfo ui = this.getUserInfoService().findUserInfoByAccount(this.account);
		if(ui == null){
			this.addFieldError("error", this.getText("error.login.user"));
			return "pop";
		}
		//判断
		if(ui.getValidated()!=1){
			this.addFieldError("error", this.getText("error.login.validated"));
			return "pop";
		}
		//判断密码
		if(!Util.hash(this.password).equalsIgnoreCase(ui.getPassword())){
			this.addFieldError("error", this.getText("error.login.user"));
			return "pop";
		}
		ui.setLastLoginTime(new Date());
		ui.setLastLoginIP(this.getRemoteAddr());
		try {
			this.getUserInfoService().updateUserInfo(ui);
		} catch (PiaoyiException e) {
            this.addActionError(this.getText("error.login.updaterror"));
            return ERROR;
        }
		UserSession us = new UserSession();
		us.setUid(ui.getId());
		us.setAccount(this.account);
		this.getSession().put(Constant.USER_SESSION_KEY, us);
		if(StringUtils.isNotBlank(this.tourl)){
			return "gourl";
		}
		return "index";
	}
}
